package main

import (
	"fmt"
	"slices"
)

const name string = "kso"

func testmain() {
	namesd()
	shuzu()
	qiepian()
	exmadin()
}
func namesd() {
	const name1 = "ksoname1"
	fmt.Println(name, name1)
}

type sd struct {
	name string
}
type sdbase struct {
	namesd string
	ss     sd
}

func shuzu() {
	var a = [5]int{0, 1, 2, 3, 4}
	fmt.Println(a)
	var b = [5][2]int{{0, 1}, {1, 2}, {2, 3}, {3, 4}}
	fmt.Println(b)
	var c [5]sd = [5]sd{{name: name}}
	fmt.Println(c)
	var d = [5]sdbase{{namesd: name, ss: sd{name: name}}}
	fmt.Println(d)
}
func qiepian() {
	var s []string
	fmt.Println("uninit:", s, s == nil, len(s) == 0)
	s = make([]string, 3)
	fmt.Println("uninit:", s, "  len:", len(s), "  caps:", cap(s))
	s[0] = "a"
	s[1] = "b"
	s[2] = "c"
	fmt.Println(s, s[2])
	fmt.Println("len:", len(s))
	s = append(s, "d")
	c := make([]string, len(s))
	copy(c, s)
	fmt.Println(c, len(c))
	t := []string{"a", "b", "c", "d"}
	if slices.Equal(t, c) {
		fmt.Println("abcd:", t)
	}
	var twoD = make([][]int, 3)
	for i := range twoD {
		twoD[i] = make([]int, 0)
	}
	fmt.Println(twoD)
}
func maptest1() {
	var m = make(map[string]int)
	m["k1"] = 7
	m["k2"] = 13
	fmt.Println(m)
	delete(m, "k2")
	clear(m)
}

func exampletest1(nums ...int) {
	fmt.Println(nums, " ===--- ")
	total := 0
	for _, num := range nums {
		total += num
	}
	fmt.Println("total:", total)
}
func exmadin() {
	exampletest1(1, 2, 3, 4, 5)
	exampletest1(1)
	nums := []int{1, 2, 3, 4}
	exampletest1(nums...)
	newints := intseq()
	fmt.Println(newints)

	add := func(x, y int) int {
		return x + y
	}
	fmt.Println("Sum:", add(3, 4))
	counter := createCounter()
	fmt.Println(counter())
	counters := createCounter()
	fmt.Println(counters())
	fmt.Println(counters())
	fmt.Println(counter())
}

// 返回一个闭包的函数
func createCounter() func() int {
	count := 0 // 定义计数变量，在闭包内被记住

	return func() int {
		count++ // 每次调用都会增加
		return count
	}
}
func intseq() func() int {
	i := 0
	fmt.Println(i)
	return func() int {
		i++
		return i
	}
}
